<?php
class UserController extends Zend_Controller_Action
{
	public function init()
	{
		$message = '请登录';
	
		switch( $this->getRequest()->getActionName() ){ // TODO sortir les actions ci-dessous dans un controller "ouvert"
			case 'index':
			case 'league-history':
			case 'league-reg-score':
			case 'confirm-game':
			case 'input-score':
			case 'league-summary':
			case 'modify-password':
			case 'cup-history':
			case 'cup-reg-score':
			case 'cup-input-score':
			case 'cup-confirm-game':
				$access = false;
				break;
			case 'login':
			case 'logout' :
				$access = true;
				break;
			
		}
		if ( !$access ){
			$auth = Zend_Auth::getInstance();
			$auth->setStorage(new Zend_Auth_Storage_Session( 'User'));
			if ( $auth->hasIdentity() )
				$this->identity = $auth->getIdentity();
			else{
				$this->_redirect('/auth/login');
			}
		}
	}
	
	public function indexAction()
	{
		$this->_redirect('/user/league-history');


		// action body
	}
	
	public function leagueHistoryAction()
	{
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
		
		$league_id = (int)$this->_getParam('league_id');
		
		$model_league_team = new Model_LeagueTeam();
		$model_league = new Model_League();
		if ($league_id==0){			
			$league_id = $model_league_team->getFirstLeagueIdByUserId($this->identity->id)->league_id;
			
		} 
		$my_leagues = $model_league_team->getLeaguesByUserId($this->identity->id);

		if($league_id!=0){
			$model_game = new Model_Game();
			$games = $model_game->getAllPlayedGamesByLeagueAndUser($league_id,$this->identity->id);
			$this->view->games = $games;
			$this->view->my_leagues = $my_leagues;
			$this->view->actuel_league_id = $league_id;	
			$this->view->actuel_league_name = $model_league->getLeagueById($league_id)->name;
		}else{
			
		}
	}
	
	public function leagueRegScoreAction()
	{
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$league_id = (int)$this->_getParam('league_id');
		$model_league_team = new Model_LeagueTeam();
		$model_league = new Model_League();
		if ($league_id==0){			
			$league_id = $model_league_team->getFirstActiveLeagueIdByUserId($this->identity->id);
			$league_id = $league_id['league_id'];			
		}
		$my_leagues = $model_league_team->getActiveLeaguesByUserId($this->identity->id);
		
		if($league_id!=0){
			$model_game = new Model_Game();
			$to_confirm_games = $model_game->getAllToConfirmGamesByLeagueAndUser($league_id,$this->identity->id);
			$new_games = $model_game->getAllNewGamesByLeagueAndUser($league_id,$this->identity->id);
			$this->view->my_leagues = $my_leagues;
			$this->view->to_confirm_games = $to_confirm_games;
			$this->view->new_games = $new_games;			
			$this->view->actuel_league_id = $league_id;
			$this->view->actuel_league_name = $model_league->getLeagueById($league_id)->name;
	
		}
	}
	
	public function confirmGameAction(){
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
		
		$game_id = (int)$this->_getParam('game-id');
		//$league_id = (int)$this->_getParam('league_id');
		$model_game = new Model_Game();
		$model_game->confirmGameByGameId($game_id);
		
		$this->_redirect('/user/league-reg-score/league_id/'.$model_game->getGameById($game_id)->league_id);
	}
		
	public function inputScoreAction(){
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
		
		$game_id = (int)$this->_getParam('game-id');
		
		if($game_id!=0){
				
			if($this->_request->isPost() ){
				$model_game = new Model_Game();
		        $model_game->updateGameScoreByUserId($game_id,
		        		                             $this->identity->id,
		        		                             $this->_request->getPost('away_team_score'),
		        									$this->_request->getPost('home_team_score')     );
				$this->_redirect('/user/league-reg-score/league_id/'.$model_game->getGameById($game_id)->league_id);
			}else{
		
			}
				
		
		
		}
	}
	
	public function leagueSummaryAction(){
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
		
		$league_id = (int)$this->_getParam('league_id');
		$model_league_team = new Model_LeagueTeam();
		$model_league = new Model_League();
		if ($league_id==0){			
			$league_id = $model_league_team->getFirstLeagueIdByUserId($this->identity->id)->league_id;
				
		}
		$my_leagues = $model_league_team->getLeaguesByUserId($this->identity->id);
		
		if($league_id!=0){			
			$league = $model_league->getLeagueById($league_id);				
			
			$model_game = new Model_Game();
			$games = $model_game->getAllGamesByLeagueId($league_id); 
			
			$team_count = $league->team_count;
			$detail_teams ="";
			/* for the sorting */
			$teams = array();
			$teams_diff = array();
			$teams_got = array();
			$teams_ids = array();
			
			for($nb=1;$nb<$team_count+1;$nb++){
			    $detail['team_name'] = $model_league_team->getLeagueTeamInfoByLeagueIdAndTeamId($league_id,$nb)->team_name;
				$detail['logo'] = $model_league_team->getLeagueTeamInfoByLeagueIdAndTeamId($league_id,$nb)->logo;
			    $detail['match'] = 0;
				$detail['score'] = 0;
				$detail['win'] = 0;
				$detail['tie'] = 0;
				$detail['lose'] = 0;
				$detail['got'] = 0;
				$detail['lost'] = 0;
				$detail['diff'] = 0;
				$detail_teams[$nb] = $detail;
				$teams[$nb] = 0;
				$teams_diff[$nb] = 0;
				$teams_got[$nb] = 0;
				$teams_ids[$nb] = $nb;		
			}
			
			foreach($games as $game){
			  if($game->home_team_confirm=='yes' && $game->away_team_confirm=='yes'){
				  $detail_teams[$game->home_team_id]['match']++;
				  $detail_teams[$game->away_team_id]['match']++;
				  if($game->home_team_score>$game->away_team_score){					
					  $detail_teams[$game->home_team_id]['score'] += 3;
					  $detail_teams[$game->home_team_id]['win']++;
					  $detail_teams[$game->home_team_id]['got'] +=$game->home_team_score;
					  $detail_teams[$game->home_team_id]['lost'] +=$game->away_team_score;
					  $detail_teams[$game->home_team_id]['diff'] += $game->home_team_score - $game->away_team_score;					  
					  
					  $detail_teams[$game->away_team_id]['lose']++;
					  $detail_teams[$game->away_team_id]['got'] +=$game->away_team_score;
					  $detail_teams[$game->away_team_id]['lost'] +=$game->home_team_score;
					  $detail_teams[$game->away_team_id]['diff'] += $game->away_team_score - $game->home_team_score;
					  
					  $teams[$game->home_team_id] +=3;
					  $teams_diff[$game->home_team_id] += $game->home_team_score - $game->away_team_score;
					  $teams_got[$game->home_team_id] += $game->home_team_score;
					  $teams_diff[$game->away_team_id] += $game->away_team_score - $game->home_team_score;
					  $teams_got[$game->away_team_id] += $game->away_team_score;
				  }else if($game->home_team_score==$game->away_team_score){
				  	$detail_teams[$game->home_team_id]['score'] ++;
				  	$detail_teams[$game->home_team_id]['tie']++;
				  	$detail_teams[$game->home_team_id]['got'] +=$game->home_team_score;
				  	$detail_teams[$game->home_team_id]['lost'] +=$game->away_team_score;
				  	$detail_teams[$game->home_team_id]['diff'] += $game->home_team_score - $game->away_team_score;

				  	$detail_teams[$game->away_team_id]['score'] ++;
				  	$detail_teams[$game->away_team_id]['tie']++;
				  	$detail_teams[$game->away_team_id]['got'] +=$game->away_team_score;
				  	$detail_teams[$game->away_team_id]['lost'] +=$game->home_team_score;
				  	$detail_teams[$game->away_team_id]['diff'] += $game->away_team_score - $game->home_team_score;
				  
				  	$teams[$game->home_team_id] ++;
				  	$teams[$game->away_team_id] ++;
					$teams_diff[$game->home_team_id] += $game->home_team_score - $game->away_team_score;
					$teams_got[$game->home_team_id] += $game->home_team_score;
					$teams_diff[$game->away_team_id] += $game->away_team_score - $game->home_team_score;
					$teams_got[$game->away_team_id] += $game->away_team_score;
					
				  }else if($game->home_team_score<$game->away_team_score){				  				  	
				  	$detail_teams[$game->home_team_id]['lose']++;
				  	$detail_teams[$game->home_team_id]['got'] +=$game->home_team_score;
				  	$detail_teams[$game->home_team_id]['lost'] +=$game->away_team_score;
				  	$detail_teams[$game->home_team_id]['diff'] += $game->home_team_score - $game->away_team_score;
				  	
				  	$detail_teams[$game->away_team_id]['score'] += 3;
				  	$detail_teams[$game->away_team_id]['win']++;
				  	$detail_teams[$game->away_team_id]['got'] +=$game->away_team_score;
				  	$detail_teams[$game->away_team_id]['lost'] +=$game->home_team_score;
				  	$detail_teams[$game->away_team_id]['diff'] += $game->away_team_score - $game->home_team_score;
				  	
				  	$teams[$game->away_team_id] +=3;
					$teams_diff[$game->home_team_id] += $game->home_team_score - $game->away_team_score;
					$teams_got[$game->home_team_id] += $game->home_team_score;
					$teams_diff[$game->away_team_id] += $game->away_team_score - $game->home_team_score;
					$teams_got[$game->away_team_id] += $game->away_team_score;
				  }
			  }
			
			}
		//arsort($teams);
			array_multisort($teams,SORT_DESC,SORT_NUMERIC ,$teams_diff,SORT_DESC,SORT_NUMERIC ,$teams_got,SORT_DESC,SORT_NUMERIC ,$teams_ids );
		
		foreach ($teams_ids as $id ){
			$detail_teams_dup[] = $detail_teams[$id];
		}
			$this->view->my_leagues = $my_leagues;
			$this->view->detail_teams = $detail_teams_dup;
			$this->view->actuel_league_name = $model_league->getLeagueById($league_id)->name;
		}
	}
	
	
	
	public function modifyPasswordAction(){
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$user_id = $this->identity->id;
		
		$form = new Form_CreateUser();
		$form->setAction($this->_request->getRequestUri());
	
		$form->username->disabled = 'disabled';
		$form->username->setValue($this->identity->username);
		$form->password->setLabel('新密码');
		$form->submit->setLabel('修改');
	
		if($this->_request->isPost()  && $this->_request->getPost( 'cible' ) == 'create'){
	
			if ($form->isValidPartial( $this->_request->getPost() )) {
	
				$model_user = new Model_User();
				$model_user->updateUserPassword($user_id,$this->_request->getPost('password'));
				 
	
				$this->_redirect('/user/league-history/');
			}
	
		}
	
		$this->view->form = $form;
	}
		 
	
	
	//////////////////////////////cups//////////////////////////
	
	public function cupHistoryAction()
	{
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$cup_id = (int)$this->_getParam('cup_id');
	
		$model_cup_team = new Model_CupTeam();
		$model_cup = new Model_Cup();
		if ($cup_id==0){
			$cup_id = $model_cup_team->getFirstCupIdByUserId($this->identity->id)->cup_id;
				
		}
		$my_cups = $model_cup_team->getCupsByUserId($this->identity->id);
	
		if($cup_id!=0){
			$model_cup_game = new Model_CupGame();
			$model_cup_ko_game = new Model_CupKoGame();
			$cup_games = $model_cup_game->getAllPlayedGamesByCupAndUser($cup_id,$this->identity->id);
			$group_name = $model_cup_team->getGroupNameByCupIdAndUser($cup_id,$this->identity->id);
			
			$this->view->games = $cup_games;
			$this->view->my_cups = $my_cups;
			$this->view->actuel_cup_id = $cup_id;
			$this->view->actuel_league_name = $model_cup->getCupById($cup_id)->name;
			$this->view->group_name = $group_name->group_name;
			
		}else{
				
		}
	}
	
	
	public function cupRegScoreAction()
	{
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$cup_id = (int)$this->_getParam('cup_id');
		$model_cup = new Model_Cup();
		$model_cup_team = new Model_CupTeam();
		if ($cup_id==0){
			$cup_id = $model_cup_team->getFirstActiveCupIdByUserId($this->identity->id);
			$cup_id = $cup_id['cup_id'];
	
		}
		$my_cups = $model_cup_team->getActiveCupsByUserId($this->identity->id);
	
		if($cup_id!=0){
			$model_cup_game = new Model_CupGame();
			$model_cup_ko_game = new Model_CupKoGame();
			$to_confirm_games = $model_cup_game->getAllToConfirmGamesByCupAndUser($cup_id,$this->identity->id);
			$new_games = $model_cup_game->getAllNewGamesByCupAndUser($cup_id,$this->identity->id);
			$group_name = $model_cup_team->getGroupNameByCupIdAndUser($cup_id,$this->identity->id);
			$this->view->my_cups = $my_cups;
			$this->view->to_confirm_games = $to_confirm_games;
			$this->view->new_games = $new_games;
			$this->view->actuel_cup_id = $cup_id;
			$this->view->actuel_league_name = $model_cup->getCupById($cup_id)->name;
			$this->view->group_name = $group_name->group_name;
			
		
			
	
		}
	}
	
	public function cupSummaryAction(){
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
		

		$cup_id = (int)$this->_getParam('cup_id');
	
		$model_cup_team = new Model_CupTeam();
		$model_cup = new Model_Cup();
		if ($cup_id==0){
			$cup_id = $model_cup_team->getFirstCupIdByUserId($this->identity->id)->cup_id;
				
		}
		$my_cups = $model_cup_team->getCupsByUserId($this->identity->id);
		
		if($cup_id!=0){
		
			$cup = $model_cup->getCupById($cup_id);
		
			$model_cup_game = new Model_CupGame();
				
			$groups = $model_cup_team->getGroupsByCupId($cup_id);
				
			$details = "";
			foreach($groups as $group){
				$num = $model_cup_team->getTeamCountByCupIdAndGroupName($cup_id,$group['group_name']);
				$details[$group['group_name']] = $this->sortCupGames($model_cup_game->getGamesByCupIdAndGroupId($cup_id,$group['group_name']),
						$num['num'], $model_cup_team, $cup_id,$group['group_name']);
			}
				
				
			$this->view->cups = $my_cups;
			$this->view->detail_teams = $details;
			$this->view->actuel_cup_name = $cup->name;
			$this->view->actuel_cup_id = $cup_id;
		}
		
	}
	
	public function cupInputScoreAction(){
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$game_id = (int)$this->_getParam('game-id');
	
		if($game_id!=0){
	
			if($this->_request->isPost() ){
				$model_cup_game = new Model_CupGame();
				$model_cup_game->updateGameScoreByUserId($game_id,
						$this->identity->id,
						$this->_request->getPost('away_team_score'),
						$this->_request->getPost('home_team_score')     );
				$this->_redirect('/user/cup-reg-score/cup_id/'.$model_cup_game->getGameById($game_id)->cup_id);
			}else{
	
			}
	
	
	
		}
	}
	
	public function koCupInputScoreAction(){
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$game_id = (int)$this->_getParam('game-id');
	
		if($game_id!=0){
	
			if($this->_request->isPost() ){
				$model_cup_ko_game = new Model_CupKoGame();
				$model_cup_ko_game->updateGameScoreByUserId($game_id,
						$this->identity->id,
						$this->_request->getPost('away_team_score'),
						$this->_request->getPost('home_team_score')     );
				$this->_redirect('/user/cup-ko-reg-score/cup_id/'.$model_cup_ko_game->getGameById($game_id)->cup_id);
			}else{
	
			}
	
	
	
		}
	}
	
	
	public function cupConfirmGameAction(){
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$game_id = (int)$this->_getParam('game-id');
		//$league_id = (int)$this->_getParam('league_id');
		$model_cup_game = new Model_CupGame();
		$model_cup_game->confirmGameByGameId($game_id);
	
		$this->_redirect('/user/cup-reg-score/cup_id/'.$model_cup_game->getGameById($game_id)->cup_id);
	}
	
	public function koCupConfirmGameAction(){
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$game_id = (int)$this->_getParam('game-id');
		//$league_id = (int)$this->_getParam('league_id');
		$model_cup_ko_game = new Model_CupKoGame();
		$model_cup_ko_game->confirmGameByGameId($game_id);
	
		$this->_redirect('/user/cup-reg-score/cup_id/'.$model_cup_ko_game->getGameById($game_id)->cup_id);
	}
	
	public function cupKoRegScoreAction()
	{
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$cup_id = (int)$this->_getParam('cup_id');
		$model_cup_ko = new Model_CupKo();
		$model_cup_ko_team = new Model_CupKoTeam();
		if ($cup_id==0){
			$cup_id = $model_cup_ko_team->getFirstActiveCupIdByUserId($this->identity->id);	
			$cup_id = $cup_id['cup_ko_id'];
	
		}
		$my_cups = $model_cup_ko_team->getActiveCupsByUserId($this->identity->id);

		if($cup_id!=0){
			$model_cup_ko_game = new Model_CupKoGame();
			$cup = $model_cup_ko->getCupKoById($cup_id);
			
			$round = 1;
			$node = 1;
			$team_count = $cup->team_count;
			while( $team_count!=1){
				for($i=0;$i<floor($team_count/2);$i++){
					$to_confirm_games[$round] = $model_cup_ko_game->getAllToConfirmGamesByCupAndUser($cup_id,$round,$this->identity->id);
					$to_start_games[$round] = $model_cup_ko_game->getAllNewGamesByCupAndUser($cup_id,$round,$this->identity->id);
						
					$node++;
				}
				$team_count = ceil($team_count/2);
				$round++;
			}
			
		
			$this->view->my_cups = $my_cups;
			$this->view->to_confirm_games = $to_confirm_games;
			$this->view->new_games = $to_start_games;
			$this->view->actuel_cup_id = $cup_id;
			$this->view->actuel_cup_name = $cup->name;
			
				
	
				
	
		}
	}
	
	public function cupKoHistoryAction()
	{
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$cup_id = (int)$this->_getParam('cup_id');
	
		$model_cup_ko_team = new Model_CupKoTeam();
		$model_cup_ko = new Model_CupKo();
		if ($cup_id==0){
			$cup_id = $model_cup_ko_team->getFirstCupIdByUserId($this->identity->id)->cup_ko_id;
	
		} 
		$my_cups = $model_cup_ko_team->getCupsByUserId($this->identity->id);
	
		if($cup_id!=0){
			
			$model_cup_ko_game = new Model_CupKoGame();
			$cup = $model_cup_ko->getCupKoById($cup_id);
			
			$round = 1;
			$node = 1;
			$team_count = $cup->team_count;
			while( $team_count!=1){
				for($i=0;$i<floor($team_count/2);$i++){
					$cup_games[$round] = $model_cup_ko_game->getAllPlayedGamesByCupAndUser($cup_id,$round,$this->identity->id);
						
					$node++;
				}
				$team_count = ceil($team_count/2);
				$round++;
			}
			
			$this->view->games = $cup_games;
			$this->view->my_cups = $my_cups;
			$this->view->actuel_cup_id = $cup_id;
			$this->view->actuel_cup_name = $cup->name;
				
		}else{
	
		}
	}
	

	public function cupKoConfirmGameAction(){
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$game_id = (int)$this->_getParam('game-id');
		//$league_id = (int)$this->_getParam('league_id');
		$model_cup_ko_game = new Model_CupKoGame();
		$model_cup_ko_game->confirmGameByGameId($game_id);
	
		$this->_redirect('/user/cup-ko-reg-score/cup_id/'.$model_cup_ko_game->getGameById($game_id)->cup_id);
	}
	
	
	
	public function cupKoSummaryAction(){
		$this->_helper->actionStack('normal', 'header');
		$this->_helper->actionStack('options', 'left-menu');
	
		$cup_id = (int)$this->_getParam('cup_id');
	
		$model_cup_ko_team = new Model_CupKoTeam();
		$model_cup_ko = new Model_CupKo();
		if ($cup_id==0){
			$cup_id = $model_cup_ko_team->getFirstCupIdByUserId($this->identity->id)->cup_ko_id;
	
		} 
		$my_cups = $model_cup_ko_team->getCupsByUserId($this->identity->id);
	
		if($cup_id!=0){
	
			$model_cup_ko_game = new Model_CupKoGame();
			$cup = $model_cup_ko->getCupKoById($cup_id);
	
			$games_org = $model_cup_ko_game->getAllGamesByCupId($cup_id);
			$games = array();
			$round = 1;
			foreach($games_org as $game){
				$game['away_team_name'] = empty($game['away_team_id'])?'':$model_cup_ko_team->getCupTeamInfoByCupIdAndTeamId($cup_id, $game['away_team_id'])->team_name;
				$game['home_team_name'] = empty($game['home_team_id'])?'':$model_cup_ko_team->getCupTeamInfoByCupIdAndTeamId($cup_id, $game['home_team_id'])->team_name;
				$games[$game['round']][] = $game;
				$round = max($game['round'],$round);
			}
	
			
			$this->view->cups = $my_cups;
			$this->view->detail_teams = $details;
			$this->view->actuel_cup_name = $cup->name;
			$this->view->actuel_cup_id = $cup_id;
			$this->view->games = $games;
			$this->view->round  = $round;
			$this->view->cup = $cup;
		}
	}
	
	
	private function sortCupGames($games,$team_count,$model_cup_team,$cup_id,$group_name){
		$detail_teams ="";
		/* for the sorting */
		$teams = array();
		$teams_diff = array();
		$teams_got = array();
		$teams_ids = array();
	
		for($nb=1;$nb<$team_count+1;$nb++){
			$detail['team_name'] = $model_cup_team->getCupTeamInfoByCupIdAndGroupNameAndTeamId($cup_id,$group_name,$nb)->team_name;
			$detail['logo'] = $model_cup_team->getCupTeamInfoByCupIdAndGroupNameAndTeamId($cup_id,$group_name,$nb)->logo;
			$detail['match'] = 0;
			$detail['score'] = 0;
			$detail['win'] = 0;
			$detail['tie'] = 0;
			$detail['lose'] = 0;
			$detail['got'] = 0;
			$detail['lost'] = 0;
			$detail['diff'] = 0;
			$detail_teams[$nb] = $detail;
			$teams[$nb] = 0;
			$teams_diff[$nb] = 0;
			$teams_got[$nb] = 0;
			$teams_ids[$nb] = $nb;
		}
	
		foreach($games as $game){
			if($game->home_team_confirm=='yes' && $game->away_team_confirm=='yes'){
				$detail_teams[$game->home_team_id]['match']++;
				$detail_teams[$game->away_team_id]['match']++;
				if($game->home_team_score>$game->away_team_score){
					$detail_teams[$game->home_team_id]['score'] += 3;
					$detail_teams[$game->home_team_id]['win']++;
					$detail_teams[$game->home_team_id]['got'] +=$game->home_team_score;
					$detail_teams[$game->home_team_id]['lost'] +=$game->away_team_score;
					$detail_teams[$game->home_team_id]['diff'] += $game->home_team_score - $game->away_team_score;
	
					$detail_teams[$game->away_team_id]['lose']++;
					$detail_teams[$game->away_team_id]['got'] +=$game->away_team_score;
					$detail_teams[$game->away_team_id]['lost'] +=$game->home_team_score;
					$detail_teams[$game->away_team_id]['diff'] += $game->away_team_score - $game->home_team_score;
	
					$teams[$game->home_team_id] +=3;
					$teams_diff[$game->home_team_id] += $game->home_team_score - $game->away_team_score;
					$teams_got[$game->home_team_id] += $game->home_team_score;
					$teams_diff[$game->away_team_id] += $game->away_team_score - $game->home_team_score;
					$teams_got[$game->away_team_id] += $game->away_team_score;
	
				}else if($game->home_team_score==$game->away_team_score){
					$detail_teams[$game->home_team_id]['score'] ++;
					$detail_teams[$game->home_team_id]['tie']++;
					$detail_teams[$game->home_team_id]['got'] +=$game->home_team_score;
					$detail_teams[$game->home_team_id]['lost'] +=$game->away_team_score;
					$detail_teams[$game->home_team_id]['diff'] += $game->home_team_score - $game->away_team_score;
	
					$detail_teams[$game->away_team_id]['score'] ++;
					$detail_teams[$game->away_team_id]['tie']++;
					$detail_teams[$game->away_team_id]['got'] +=$game->away_team_score;
					$detail_teams[$game->away_team_id]['lost'] +=$game->home_team_score;
					$detail_teams[$game->away_team_id]['diff'] += $game->away_team_score - $game->home_team_score;
	
					$teams[$game->home_team_id] ++;
					$teams[$game->away_team_id] ++;
					$teams_diff[$game->home_team_id] += $game->home_team_score - $game->away_team_score;
					$teams_got[$game->home_team_id] += $game->home_team_score;
					$teams_diff[$game->away_team_id] += $game->away_team_score - $game->home_team_score;
					$teams_got[$game->away_team_id] += $game->away_team_score;
	
				}else if($game->home_team_score<$game->away_team_score){
					$detail_teams[$game->home_team_id]['lose']++;
					$detail_teams[$game->home_team_id]['got'] +=$game->home_team_score;
					$detail_teams[$game->home_team_id]['lost'] +=$game->away_team_score;
					$detail_teams[$game->home_team_id]['diff'] += $game->home_team_score - $game->away_team_score;
	
					$detail_teams[$game->away_team_id]['score'] += 3;
					$detail_teams[$game->away_team_id]['win']++;
					$detail_teams[$game->away_team_id]['got'] +=$game->away_team_score;
					$detail_teams[$game->away_team_id]['lost'] +=$game->home_team_score;
					$detail_teams[$game->away_team_id]['diff'] += $game->away_team_score - $game->home_team_score;
	
					$teams[$game->away_team_id] +=3;
					$teams_diff[$game->home_team_id] += $game->home_team_score - $game->away_team_score;
					$teams_got[$game->home_team_id] += $game->home_team_score;
					$teams_diff[$game->away_team_id] += $game->away_team_score - $game->home_team_score;
					$teams_got[$game->away_team_id] += $game->away_team_score;
				}
			}
	
		}
	
	
		array_multisort($teams,SORT_DESC,SORT_NUMERIC ,$teams_diff,SORT_DESC,SORT_NUMERIC ,$teams_got,SORT_DESC,SORT_NUMERIC ,$teams_ids );
	
	
		foreach ($teams_ids as $id ){
			$detail_teams_dup[] = $detail_teams[$id];
		}
	
		return $detail_teams_dup;
	}
	
}